#爬取小说
import requests
import parsel


url = 'https://www.bqgbe.com/book/101816/'
reponses = requests.get(url)
#构建选择器
selector = parsel.Selector(reponses.text)
#获取小说名字,解析h1标签的文本信息
#https://www.cnblogs.com/yoyo1216/p/15817753.html
novle_name = selector.css('div.info h1::text').get()

href = selector.css('div.listmain dd a::attr(href)').getall() #获取小说章节url

for link in href:
    link_url = 'https://www.bqgbe.com' + link
    reponses_1 = requests.get(link_url)
    #获取章节名字
    selector_1 = parsel.Selector(reponses_1.text)
    #解析章节名字
    title = selector_1.css('div.content h1::text').get()
    #获取内容,注意，#是id选择器
    content_list = selector_1.css('#chaptercontent::text').getall()
    #使用join合并list数据,以换行符合并
    content = '\n'.join(content_list)
    #保存
    with open(novle_name + '.txt',mode='a',encoding='utf-8') as f:
        f.write(title)
        f.write('\n')
        f.write(content)
        f.write('\n')

